package com.itmk.web.jubao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.jubao.entity.JuBao;
import com.itmk.web.jubao.entity.JuBaoParm;
import com.itmk.web.jubao.entity.ReportVo;
import com.itmk.web.jubao.service.JuBaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/jubao")
public class JuBaoController {
    @Autowired
    private JuBaoService jubaoService;
    //举报
    @PostMapping("/jubao")
    public ResultVo jubao(@RequestBody JuBao jubao){
       //判断是否重复举报
        QueryWrapper<JuBao> query = new QueryWrapper<>();
        query.lambda().eq(JuBao::getGoodsId,jubao.getGoodsId())
                .eq(JuBao::getUserId,jubao.getUserId());
        JuBao one = jubaoService.getOne(query);
        if(one != null){
            return ResultUtils.error("您已经举报过该商品，请勿重复举报");
        }
        jubao.setCreateTime(new Date());
        if(jubaoService.save(jubao)){
            return ResultUtils.success("举报成功");
        }
        return ResultUtils.error("举报失败");
    }

    //查询举报列表
    @GetMapping("/getList")
    public ResultVo getList(JuBaoParm parm){
        IPage<ReportVo> list =  jubaoService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }


    //处理举报内容
    @PostMapping("/doJuBao")
    public ResultVo doReport(@RequestBody JuBao report){
        UpdateWrapper<JuBao> update = new UpdateWrapper<>();
        update.lambda().set(JuBao::getStatus,"1")
                .eq(JuBao::getJubaoId,report.getJubaoId());
        if(jubaoService.update(update)){
            return ResultUtils.success("处理成功!");
        }
        return ResultUtils.error("处理失败!");
    }
}
